﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformToolkit.Utils
{
    public static class DataGridViewUtils
    {
        private static DataTable CreateDataTable(int rows, int cols)
        {
            DataTable table = new DataTable();

            for (int col = 0; col < cols; col++)
            {
                table.Columns.Add();
            }

            for (int row = 0; row < rows; row++)
            {
                table.Rows.Add();
            }

            return table;
        }

        public static DataTable DataGridView2DataTable(DataGridView dataGridView)
        {
            int rows = dataGridView.Rows.Count;
            int cols = dataGridView.Columns.Count;

            DataTable table = CreateDataTable(rows, cols);

            for (int row = 0; row < rows; row++)
            {
                DataRow dataRow = table.Rows[row];

                for (int col = 0; col < cols; col++)
                {
                    object value = dataGridView[col, row].Value;
                    dataRow[col] = value;
                }
            }

            return table;
        }
    }
}
